/**
 * This file is Copyright © 2008 Software Craftsmen Limited. All Rights Reserved.
 */
package com.softwarecraftsmen.generators.summarisers;

import com.softwarecraftsmen.functors.Optional;
import static com.softwarecraftsmen.functors.Optional.None;
import static com.softwarecraftsmen.functors.Optional.Something;
import org.jetbrains.annotations.NotNull;

import java.math.BigDecimal;
import static java.math.BigDecimal.valueOf;

public class ArithmeticAverage extends Summariser<Long, Optional<BigDecimal>>
{
	private static final Long Zero = 0l;
	private static final Long One = 1l;
	private Long runningTotal;
	private Long count;

	public ArithmeticAverage()
	{
		runningTotal = Zero;
		count = Zero;
	}

	@NotNull
	public Optional<BigDecimal> summary()
	{
		if (count.equals(Zero))
		{
			return None();
		}
		return Something(valueOf(runningTotal).divide(valueOf(count)));
	}

	public void closure(final @NotNull Long value)
	{
		runningTotal += value;
		count += One;
	}
}